import Vue from 'vue'
import VueRouter from 'vue-router'

// import store from '../store/index'

// 把下面的路由 作懒加载处理
// const Login = () => import(/* webpackChunkName: "users" */ '../components/login.vue')
import welcome from '../components/welcome.vue'
import sceneryDetail from '../components/Detail/detail.vue'
import Strategy from '../views/Strategy/strategy.vue'
import Home from '../components/Home.vue'
import Tables from '../views/tables/Tables.vue'
import List from '../views/tables/list/List.vue'
import Monitor from '../views/tables/details/Details.vue'
import Setting from '../views/setting/Setting.vue'
import NotFind from '../views/404.vue'
import IndexHome from '../views/Index/scenery.vue'
import Index from '../views/home/HomeInfo.vue'
import Map from '../views/map/map.vue'
import hotelDetail from '../views/hotelDetail/hotelDetail.vue'
// import Scenery from '../components/scenery/scenery.vue'

Vue.use(VueRouter)

// 解决点击同一个路由，页面报错的问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
const routes = [
  {
    path: '/',
    component: IndexHome,
    redirect: '/index',
    children: [
      {
        path: '/index',
        name: 'index',
        component: Index
      },
      {
        path: '/map',
        name: 'map',
        component: Map
      }, {
        path: '/strategy',
        name: 'strategy',
        component: Strategy
      },
      {
        path: '/sceneryDetail',
        name: 'sceneryDetail',
        component: sceneryDetail
      },
      {
        path: '/hotelDetail',
        name: 'hotelDetail',
        component: hotelDetail
      }
    ]
  },
  {
    path: '/login',
    component: resolve => require(['../components/login.vue'], resolve)
  },
  {
    path: '/register',
    component: resolve => require(['../components/Register.vue'], resolve)
  },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    meta: {
      breadcrumb: '首页',
      requireLogin: true
    },
    children: [
      {
        path: '/welcome',
        name: 'Welcome',
        component: welcome,
        meta: {
          breadcrumb: 'dashboard',
          requireLogin: true
        }
      },

      {
        path: '/tables',
        redirect: '/tables/list',
        name: 'Tables',
        component: Tables,
        meta: {
          breadcrumb: '用户',
          requireLogin: true
        },
        children: [
          {
            path: '/tables/list',
            name: 'List',
            component: List,
            meta: {
              title: 'list',
              breadcrumb: '列表',
              requireLogin: true
            }
          },
          {
            path: '/tables/details',
            name: 'Monitor',
            component: Monitor,
            meta: {
              title: 'details',
              breadcrumb: '监控',
              requireLogin: true
            }
          }
        ]
      },

      {
        path: '/logs',
        redirect: '/logs/list',
        name: 'Logs',
        component: resolve => require(['../views/logs/Logs.vue'], resolve),
        meta: {
          breadcrumb: '日志',
          requireLogin: true
        },
        children: [
          {
            path: '/logs/list',
            name: 'List',
            component: resolve =>
              require(['../views/logs/list/List.vue'], resolve),
            meta: {
              title: 'list',
              breadcrumb: '列表',
              requireLogin: true
            }
          }
        ]
      },
      {
        path: '/scenery',
        redirect: '/scenery/list',
        name: 'scenery',
        component: resolve =>
          require(['../components/scenery/scenery.vue'], resolve),
        meta: {
          breadcrumb: '景区',
          requireLogin: true
        },
        children: [
          {
            path: '/scenery/list',
            name: 'List',
            component: resolve =>
              require(['../components/scenery/list/list.vue'], resolve),
            meta: {
              title: 'list',
              breadcrumb: '列表',
              requireLogin: true
            }
          },
          {
            path: '/scenery/add',
            name: 'List',
            component: resolve =>
              require(['../components/scenery/add.vue'], resolve),
            meta: {
              title: 'list',
              breadcrumb: '添加景区',
              requireLogin: true
            }
          },
          {

            path: '/scenery/monitor',
            name: 'Monitor',
            component: Monitor,
            meta: {
              title: 'details',
              breadcrumb: '监控',
              requireLogin: true
            }

          }
        ]
      },
      {
        path: '/notes',
        redirect: '/notes/list',
        name: 'Notes',
        component: resolve => require(['../views/notes/Notes.vue'], resolve),
        meta: {
          breadcrumb: '表格',
          requireLogin: true
        },
        children: [
          {
            path: '/notes/list',
            name: 'List',
            component: resolve =>
              require(['../views/notes/list/List.vue'], resolve),
            meta: {
              title: 'list',
              breadcrumb: '列表',
              requireLogin: true
            }
          }
        ]
      },
      {
        path: '/setting',
        name: 'Setting',
        component: Setting,
        meta: {
          breadcrumb: '设置',
          requireLogin: true
        }
      },
      {
        path: '/upload-excel',
        name: 'Upload',
        component: resolve =>
          require(['../views/excel/upload-excel.vue'], resolve),
        meta: {
          breadcrumb: '上传',
          requireLogin: true
        }
      }
    ]
  },
  {
    path: '*',
    name: 'NotFind',
    component: NotFind,
    meta: {
      title: '404'
    }
  }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //   next() 放行  next('/login') 强制跳转

  if (to.path === '/login') return next()
  if (to.path === '/map') return next()
  if (to.path === '/') return next()
  if (to.path === '/index') return next()
  if (to.path === '/register') return next()
  if (to.path === '/detail') return next()
  // 获取token
  // const tokenStr = window.sessionStorage.getItem('token')
  // if (!tokenStr) return next('/login')
  next()
})

// router.beforeEach((to, from, next) => {
//  if (to.meta.title) {
//   // 路由发生变化修改页面title
//   document.title = to.meta.title
//  }
//  if (to.meta.requireLogin) {
//   if (store.getters.token) {
//    if (Object.keys(from.query).length === 0) {
//     // 判断路由来源是否有query，处理不是目的跳转的情况
//     next()
//    } else {
//     let redirect = from.query.redirect // 如果来源路由有query
//     if (to.path === redirect) {
//      // 避免 next 无限循环
//      next()
//     } else {
//      next({
//       path: redirect
//      }) // 跳转到目的路由
//     }
//    }
//   } else {
//    next({
//     path: '/login',
//     query: {
//      redirect: to.fullPath
//     } // 将跳转的路由path作为参数，登录成功后跳转到该路由
//    })
//   }
//  } else {
//   next()
//  }
// })

export default router
